package com.example.dps;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;



public class EditAccountActivity extends Activity{
	
	AccountDBHelper accountDBHelper;
	
	WebHelper webHelper;
	
	Account account = new Account();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_account_layout);
		
		this.accountDBHelper = new AccountDBHelper(this);
		
		this.webHelper = new WebHelper(this);
		
		
		//addButtonListeners();
		
		Handler handler=new Handler();
		Runnable r=new Runnable()
		{
		    public void run() 
		    {
		    	ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
         	   	sv.fullScroll(ScrollView.FOCUS_UP); 			
		    }
		};
		handler.postDelayed(r, 10);
		
		
		
		restoreFormInfo();
		
		addButtonClickListeners();
		
		ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView);
		if(sv!=null) sv.scrollTo(0,0);
		

	}
	
	public void onResume(){
		super.onResume();
		
		ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView);
		if(sv!=null) sv.scrollTo(0,0);
		
		this.webHelper.authenticate("editAccount");
		
		this.webHelper.getAccountInfo();
		
		
		restoreFormInfo();
		
		
	}
	
	
	public void restoreFormInfo(){
		
		
		Bundle extras = this.getIntent().getExtras();
		
		Cursor accountCursor = this.accountDBHelper.getAccountAsCursor(extras.getString("accountName"));
		
		String fullName;
		String birthdate;
		String socialSecurity;
		String employer;
		String phoneNumber;
		String homeAddress;
		String homeCity;
		String homeState;
		String homeZip;
		
		String routeNum;
		String checkNum;
		
		if(accountCursor.moveToFirst()){
			
			fullName = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_FULLNAME));
			birthdate = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_BIRTHDATE));
			socialSecurity = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_SOCIAL_SECURITY));
			employer = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_EMPLOYER));
			phoneNumber = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_PHONE_NUMBER));
			homeAddress = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_HOME_ADDRESS));
			homeCity = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_HOME_CITY));
			homeState = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_HOME_STATE));
			homeZip = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_HOME_ZIP));
			checkNum = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_CHECKNUM));
			routeNum = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_ROUTENUM));
			
			
			((EditText) findViewById(R.id.editAccountNameValue)).setText(fullName);
			((EditText) findViewById(R.id.editAccountBirthDateValue)).setText(birthdate);
			((EditText) findViewById(R.id.editAccountSocialSecurityValue)).setText(socialSecurity);
			((EditText) findViewById(R.id.editAccountEmployerValue)).setText(employer);
			((EditText) findViewById(R.id.editAccountPhoneNumberValue)).setText(phoneNumber);
			((EditText) findViewById(R.id.editAccountHomeAddressValue)).setText(homeAddress);
			((EditText) findViewById(R.id.editAccountHomeCityValue)).setText(homeCity);
			((EditText) findViewById(R.id.editAccountHomeStateValue)).setText(homeState);
			((EditText) findViewById(R.id.editAccountHomeZipValue)).setText(homeZip);
			
			((EditText) findViewById(R.id.editAccountAccountNumber)).setText(checkNum);
			((EditText) findViewById(R.id.editAccountRoutingNumber)).setText(routeNum);
			
			
			/** update the account object to hold this info **/
			this.account.id = accountCursor.getInt(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_ID));
			this.account.name = accountCursor.getString(accountCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_NAME));
			
			this.account.fullName = fullName;
			this.account.birthdate = birthdate;
			this.account.socialSecurity = socialSecurity;
			this.account.employer = employer;
			this.account.phoneNumber = phoneNumber;
			this.account.homeAddress = homeAddress;
			this.account.homeState = homeState;
			this.account.homeCity = homeCity;
			this.account.homeZip = homeZip;
			
			this.account.checkNum = checkNum;
			this.account.routeNum = routeNum;
			
			
		}
		
		
		
		
		
	}
	
	public void addButtonClickListeners(){
		
		ImageView continueButton = (ImageView) findViewById(R.id.editContinueBtn);
		
		continueButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View view) {
				
				updateWorkingAccount();
				
				accountDBHelper.editAccount(account);
				
				Toast.makeText(EditAccountActivity.this, "Updated Account Info", Toast.LENGTH_LONG).show();
				
				EditAccountActivity.this.finish();
				
			}
			
		});
		
	}
	
	/** update the current account that the edit activity is workign with **/
	public void updateWorkingAccount(){
		
		this.account.fullName = ((EditText) findViewById(R.id.editAccountNameValue)).getText().toString();
		this.account.birthdate = ((EditText) findViewById(R.id.editAccountBirthDateValue)).getText().toString();
		this.account.socialSecurity = ((EditText) findViewById(R.id.editAccountSocialSecurityValue)).getText().toString();
		this.account.employer = ((EditText) findViewById(R.id.editAccountEmployerValue)).getText().toString();
		this.account.phoneNumber = ((EditText) findViewById(R.id.editAccountPhoneNumberValue)).getText().toString();
		this.account.homeAddress = ((EditText) findViewById(R.id.editAccountHomeAddressValue)).getText().toString();
		this.account.homeCity = ((EditText) findViewById(R.id.editAccountHomeCityValue)).getText().toString();
		this.account.homeState = ((EditText) findViewById(R.id.editAccountHomeStateValue)).getText().toString();
		this.account.homeZip = ((EditText) findViewById(R.id.editAccountHomeZipValue)).getText().toString();
		
		
		this.account.checkNum = ((EditText) findViewById(R.id.editAccountAccountNumber)).getText().toString();
		this.account.routeNum = ((EditText) findViewById(R.id.editAccountRoutingNumber)).getText().toString();
		
		this.accountDBHelper.updateAccount(account);
		
		Account check = this.accountDBHelper.getAccountAsAccount("Checking Account");
		
		Log.e("UPDATEWORKINGACCOUNT", check.fullName + " = FULL NAME" );
		
		webHelper.updateAccountInfo();
		
		
	}
	

}
